﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif

[ExecuteInEditMode]
public class EntityGroundTx : MonoBehaviour
{
    [Tooltip("地板中间填充色")]
    public List<Color> colorCenter = new List<Color>();
    [Tooltip("地板阴影朝下填充色")]
    public List<Color> colorShadowBottom = new List<Color>();
    [Tooltip("地板阴影朝左填充色")]
    public List<Color> colorShadowLeft = new List<Color>();
    [Tooltip("地板阴影朝左下填充色")]
    public List<Color> colorShadowAll = new List<Color>();

    [Tooltip("地板纹理导出名")]
    public int name = 0;
    [Tooltip("地板纹理所处九方idx 1~9")]
    public int idx = 5;
    [Tooltip("地板纹理尺寸")]
    public int size = 10;
    [Tooltip("地板纹理随机区域尺寸")]
    public int dis = 2;
    [Tooltip("地板纹理随机区域尺寸 每圈填充数")]
    public Dictionary<int, int> disColorNum = new Dictionary<int, int>();

    private Dictionary<int, int> _disColorNum = new Dictionary<int, int>();
    private Dictionary<int, bool> texColors = new Dictionary<int, bool>();
    // Update is called once per frame
    public void Expoter()
    {
#if UNITY_EDITOR
        texColors.Clear();
        int tx_size = size + dis * 2;
        Texture2D t = new Texture2D(tx_size, tx_size, TextureFormat.RGBAFloat, false, false);
        float p_dis = Mathf.Pow(2, dis);
        // 粗略填充 bool texColors
        for (int i = 0; i < tx_size; i++)
        {
            for (int j = 0; j < tx_size; j++)
            {
                if ((i >= dis && j >= dis) && (i <= dis + size && j <= dis + size))
                {
                    int key = i + j * tx_size;
                    texColors[key] = true;
                }
                else
                {
                    int x = i % size;
                    int i_x = i / size;
                    int dis_x = i_x > 0 ? x : (dis - x);
                    int y = j % size;
                    int j_y = j / size;
                    int dis_y = j_y > 0 ? y : (dis - y);
                    int cur_dis = Mathf.Max(dis_x, dis_y);
                    float n = Random.Range(0, p_dis - 1);
                    float dis_n = p_dis / Mathf.Pow(2, cur_dis);
                    bool bColor = n < (dis / dis_n);
                    int cur_num = _disColorNum[cur_dis];
                    int max_num = 100;
                    if (disColorNum.ContainsKey(cur_dis)) max_num = disColorNum[cur_dis];
                    if (bColor && cur_num <= max_num)
                    {
                        _disColorNum[cur_dis] = cur_num + 1;
                        int key = i + j * tx_size;
                        texColors[key] = true;
                    }
                }
            }
        }
        // 精确填充 Color Texture2D
        foreach (int key in texColors.Keys)
        {
            int x = key % tx_size;
            int y = key / tx_size;
            Color c = GetColor(x, y);
            t.SetPixel(x, y, c);
        }
        // 保存
#endif
    }

    Color GetColor(int x, int y)
    {
        Color c = new Color();
        // 检查像素 下 是否填充
        bool b_bottom = GetIsColor(x, y - 1);
        // 检查像素 左 是否填充
        bool b_left = GetIsColor(x - 1, y);
        if (!b_bottom && !b_left)
        {
            int idx = Random.Range(0, colorShadowAll.Count - 1);
            c = colorShadowAll[idx];
        }
        else if (!b_left)
        {
            int idx = Random.Range(0, colorShadowLeft.Count - 1);
            c = colorShadowLeft[idx];
        }
        else if (!b_bottom)
        {
            int idx = Random.Range(0, colorShadowBottom.Count - 1);
            c = colorShadowBottom[idx];
        }
        else
        {
            int idx = Random.Range(0, colorCenter.Count - 1);
            c = colorCenter[idx];
        }
        return c;
    }

    bool GetIsColor(int x, int y)
    {
        bool isColor = false;
        int tx_size = size + dis * 2;
        int key = x + y * tx_size;
        isColor = texColors.ContainsKey(key) && texColors[key];
        return isColor;
    }
}
